#!/usr/bin/env python

from unittest import TestCase, main
import numpy
import random
import sys
sys.path += ['..']
import exact_diagonalization

class test_( TestCase ):
    '''
    '''
    def test_i2xy(self):
        N = 2
        i2xy = exact_diagonalization.i2xy
        self.assertEqual(i2xy(0, N), (0,0) )
        self.assertEqual(i2xy(3, N), (1,1) )
        
    def test_i2state(self):
        i2state = exact_diagonalization.i2state
        N = 2
        self.assertEqual( i2state(0, 2**N ), [-1, -1, -1, -1])
        self.assertEqual( i2state(15, 2**N), [1, 1, 1, 1])
        self.assertEqual( i2state(4, 2**N), [-1, 1, -1, -1] )
        
    def test_state2i(self):
        state2i = exact_diagonalization.state2i
        self.assertEqual( state2i([1, 1]), 3 )
        self.assertEqual( state2i([1, 1, 1, 1]), 15 )
        
    def test_xy2i(self):
        N = 2
        xy2i = exact_diagonalization.xy2i
        self.assertEqual( xy2i( 0,0, N ), 0 )
        self.assertEqual( xy2i( 1,1, N ), 3 )
        
    def test_Hamiltonian_neighbors(self):
        neighbors = ( (0, 1), (0, -1), (1, 0), (-1, 0) )
        N = 2
        H = exact_diagonalization.Hamiltonian( neighbors, N )
        self.assertEqual(H.xy_neighbors, {(0, 1): [(0, 0), (0, 0), (1, 1), (1, 1)], 
                                          (1, 0): [(1, 1), (1, 1), (0, 0), (0, 0)], 
                                          (0, 0): [(0, 1), (0, 1), (1, 0), (1, 0)], 
                                          (1, 1): [(1, 0), (1, 0), (0, 1), (0, 1)]} )
        self.assertEqual( H.i_neighbors, {0: [2, 2, 1, 1], 
                                          1: [3, 3, 0, 0], 
                                          2: [0, 0, 3, 3], 
                                          3: [1, 1, 2, 2]} )
        
        neighbors = ( (0, 1), (0, -1), (1, 0), (-1, 0), (-1, -1), (1, 1))
        H = exact_diagonalization.Hamiltonian( neighbors, N )
        self.assertEqual( H.xy_neighbors, {(0, 1): [(0, 0), (0, 0), (1, 1), (1, 1), (1, 0), (1, 0)], 
                                           (1, 0): [(1, 1), (1, 1), (0, 0), (0, 0), (0, 1), (0, 1)], 
                                           (0, 0): [(0, 1), (0, 1), (1, 0), (1, 0), (1, 1), (1, 1)], 
                                           (1, 1): [(1, 0), (1, 0), (0, 1), (0, 1), (0, 0), (0, 0)]} )
        self.assertEqual(H.i_neighbors, {0: [2, 2, 1, 1, 3, 3], 
                                          1: [3, 3, 0, 0, 2, 2], 
                                          2: [0, 0, 3, 3, 1, 1], 
                                          3: [1, 1, 2, 2, 0, 0]})
        
        N = 3
        H = exact_diagonalization.Hamiltonian( neighbors, N )
        self.assertEqual(H.xy_neighbors, {(0, 1): [(0, 2), (0, 0), (1, 1), (2, 1), (2, 0), (1, 2)], 
                                          (1, 2): [(1, 0), (1, 1), (2, 2), (0, 2), (0, 1), (2, 0)], 
                                          (0, 0): [(0, 1), (0, 2), (1, 0), (2, 0), (2, 2), (1, 1)], 
                                          (2, 1): [(2, 2), (2, 0), (0, 1), (1, 1), (1, 0), (0, 2)], 
                                          (1, 1): [(1, 2), (1, 0), (2, 1), (0, 1), (0, 0), (2, 2)], 
                                          (2, 0): [(2, 1), (2, 2), (0, 0), (1, 0), (1, 2), (0, 1)], 
                                          (2, 2): [(2, 0), (2, 1), (0, 2), (1, 2), (1, 1), (0, 0)], 
                                          (1, 0): [(1, 1), (1, 2), (2, 0), (0, 0), (0, 2), (2, 1)], 
                                          (0, 2): [(0, 0), (0, 1), (1, 2), (2, 2), (2, 1), (1, 0)]} )
        self.assertEqual( H.i_neighbors, {0: [3, 6, 1, 2, 8, 4], 
                                          1: [4, 7, 2, 0, 6, 5], 
                                          2: [5, 8, 0, 1, 7, 3], 
                                          3: [6, 0, 4, 5, 2, 7], 
                                          4: [7, 1, 5, 3, 0, 8], 
                                          5: [8, 2, 3, 4, 1, 6], 
                                          6: [0, 3, 7, 8, 5, 1],
                                          7: [1, 4, 8, 6, 3, 2], 
                                          8: [2, 5, 6, 7, 4, 0]} )
        
    def test_sum_neighbors(self):
        neighbors = ( (0, 1), (0, -1), (1, 0), (-1, 0) )
        N = 2
        H = exact_diagonalization.Hamiltonian( neighbors, N )
        self.assertEqual( H.sum_neighbors(0, [-1, -1, -1, -1]), -4)
        self.assertEqual( H.sum_neighbors(0, [-1, 1, 1, -1]), 4)
        self.assertEqual( H.sum_neighbors(0, [-1, 1, 1, -1]), H.sum_neighbors(0, [1, 1, 1, -1]) )
        
    def test_matrix_gen(self):
        '''
        checking if our matrix is symmetric
        '''
        neighbors = ( (0, 1), (0, -1), (1, 0), (-1, 0), (-1, -1), (1, 1))
        #neighbors = ( (0, 1), (0, -1), (1, 0), (-1, 0) )
        N = 2
        H = exact_diagonalization.Hamiltonian( neighbors, N ).matrix_gen(3,4)
        if numpy.all(H == H.T):
            self.assertEqual(1, 1)
        
if __name__ == '__main__':
    main()
